package com.hibiscusking.basic.controller;

import com.hibiscusking.basic.config.BussinessException;
import com.hibiscusking.basic.dto.PhoneVerificationDto;
import com.hibiscusking.basic.service.VerifyCodeService;
import com.hibiscusking.basic.util.AjaxResult;
import com.hibiscusking.basic.util.FastdfsUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;
    /**
     * 获取图像验证码
     * */
    @GetMapping("/image/{key}")
    public AjaxResult uploadFile(@PathVariable("key")String key)  {
        String verification = verifyCodeService.pictureVerification(key);
        return AjaxResult.me().success(verification);
    }
    /**
     * 获取短信验证码
     * */
    @PostMapping("/smsCode")
    public AjaxResult sendOutVerification(@RequestBody @Valid PhoneVerificationDto dto) {
        verifyCodeService.sendOutVerification(dto);
        return AjaxResult.me().success();
    }
    /**
     * 获取邮箱验证码
     * */
    @GetMapping("/emailCode/{key}")
    public AjaxResult emailCode(@PathVariable("key")String key)  {
        if (StringUtils.isBlank(key)) {
            throw new BussinessException("邮箱不能为空");
        }
        verifyCodeService.sendOutEmailVerification(key);
        return AjaxResult.me().success();
    }
}
